package com.example.androidcomplete02.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.androidcomplete02.R;

public class SaveFragmentState extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_savefragmentstate);
	}

	public static class CounterFragment extends Fragment {
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View root = inflater.inflate(R.layout.counterfrg, container, false);
			Button btnIncrease = (Button) root.findViewById(R.id.btnincrease);
			final TextView textCounter = (TextView) root
					.findViewById(R.id.txtcounter);

			if (savedInstanceState != null) {
				textCounter.setText(Integer.toString(savedInstanceState
						.getInt("counter")));
			}

			btnIncrease.setOnClickListener(new Button.OnClickListener() {

				@Override
				public void onClick(View v) {
					int count = Integer.parseInt(textCounter.getText()
							.toString());
					textCounter.setText(Integer.toString(count + 1));
				}

			});
			return root;
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);

			TextView textCounter = (TextView) getView().findViewById(
					R.id.txtcounter);
			int a = Integer.parseInt(textCounter.getText().toString());
			outState.putInt("counter", a);
		}
	}
}
